﻿Imports Negocio
Public Class frmPedido
    Public Oferta As Oferta
    Private suma As Integer = 60
    Private listaProductos As New List(Of ProductoPedido)
    Private total As Decimal
    Private antcbo As New ComboBox
    Private antbut As New Button
    Private antText As New TextBox
    Private existe As Boolean = False


    Private Sub frmPedido_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If Not IsNothing(Oferta) Then
            Dim cbo As New ComboBox
            cbo.Name = "cbo"
            cbo.Enabled = False
            cbo.Location = New Point(29, suma)
            cbo.Text = Oferta.Producto
            cbo.Size = New Size(181, 21)
            Me.Controls.Add(cbo)

            Dim text As New TextBox
            text.Name = "txt"
            text.Size = New Size(56, 20)
            text.Location = New Point(272, suma)
            text.Text = Oferta.Cantidad
            text.Enabled = False
            Me.Controls.Add(text)

            Dim butOf As New Button
            butOf.Size = New Size(56, 20)
            butOf.Location = New Point(358, suma)
            butOf.Text = "Eliminar"
            butOf.Name = "butOf"
            butOf.Visible = True
            butOf.Enabled = True
            AddHandler butOf.Click, AddressOf btnof_Click
            Me.Controls.Add(butOf)
            suma = suma + 70

        End If
    End Sub
    Private Sub btnof_Click(sender As System.Object, e As System.EventArgs)
        For h As Integer = 0 To Me.Controls.Count - 1
            If Me.Controls(h).Name = "cbo" Then
                Me.Controls.RemoveAt(h)
                Me.Controls.RemoveAt(h + 1)
                Me.Controls.RemoveAt(h + 2)
                Exit For
            End If
        Next
        Oferta = Nothing
        suma = suma - 70
        For i As Integer = 0 To Me.Controls.Count - 1
            If Me.Controls(i).Name = "cbo" Then
                Dim cbo As ComboBox = Me.Controls(i)
                Dim text As TextBox = Me.Controls(i + 1)
                Dim btn As Button = Me.Controls(i + 2)
                cbo.Location = New Point(29, suma)
                text.Location = New Point(272, suma)
                btn.Location = New Point(358, suma)
            End If
        Next




    End Sub

    
    Private Sub btn_Click(sender As System.Object, e As System.EventArgs)

        Dim num As Integer
        Dim but As Button = sender
        For i As Integer = 0 To Me.Controls.Count - 1
            If Me.Controls(i).Equals(but) Then
                Me.Controls.RemoveAt(i)
                Me.Controls.RemoveAt(i - 1)
                Me.Controls.RemoveAt(i - 2)
                num = i
                suma = suma - 70
                Exit For
            End If

        Next
        For j As Integer = num + 1 To Me.Controls.Count - 1
            If Me.Controls(j).Name = "cbo" Then
                Me.Controls(j).Location = New Point(29, suma)
                Me.Controls(j + 1).Location = New Point(272, suma)
                Me.Controls(j + 2).Location = New Point(358, suma)

            End If
        Next
        Dim controls1() As Control = Me.Controls.Find("cbo", False)
        If controls1.Count = 0 Then
            existe = False
        End If

    End Sub

    Private Sub btnAceptar_Click(sender As System.Object, e As System.EventArgs) Handles btnAceptar.Click
        If existe = True Then
            Dim ped As New ProductoPedido(antcbo.SelectedItem, Integer.Parse(antText.Text))
            antbut.Tag = ped
        End If

        If Not IsNothing(Oferta) Then
            total = Oferta.Precio
        End If
        Dim controls() As Control = Me.Controls.Find("but", False)
        For i As Integer = 0 To controls.Count - 1
            Dim ped As ProductoPedido
            ped = controls(i).Tag
            listaProductos.Add(ped)

        Next
        Dim pedi As New Pedido(miUsuario.Nombre, Today, listaProductos)
        total = total + pedi.PrecioTotal
        txtTotal.Text = total
        Dim sino As DialogResult = MessageBox.Show("El precio total del pedido mas la oferta (si la hubiera), es de " & total & " ¿ Confirmas el pedido ? ", "Atencion", MessageBoxButtons.YesNo)
        If sino = Windows.Forms.DialogResult.No Then
            total = 0
            txtTotal.Text = ""
            listaProductos.Clear()
            Exit Sub
        Else
            Dim mens As String = miGestionPedidos.Añadir(pedi)
            If mens <> "" Then
                MessageBox.Show(mens)
                total = 0
                txtTotal.Text = ""
                listaProductos.Clear()
                Exit Sub
            Else
                MessageBox.Show("Su pedido se ha realizado correctamente")
            End If


        End If
    End Sub

    Private Sub btnPlus_Click_1(sender As System.Object, e As System.EventArgs) Handles btnPlus.Click
        If existe = True Then
            Dim ped As New ProductoPedido(antcbo.SelectedItem, Integer.Parse(antText.Text))
            antbut.Tag = ped
        End If
        
        Dim cbo As New ComboBox
        cbo.Name = "cbo"
        cbo.Enabled = True
        cbo.Location = New Point(29, suma)
        For i As Integer = 0 To miGestionProductos.Productos.Count - 1
            cbo.Items.Add(miGestionProductos.Productos(i).Nombre)
        Next
        cbo.Size = New Size(181, 21)
        Me.Controls.Add(cbo)

        Dim text As New TextBox
        text.Name = "txt"
        text.Size = New Size(56, 20)
        text.Location = New Point(272, suma)
        text.Enabled = True
        Me.Controls.Add(text)


        Dim but As New Button
        but.Size = New Size(56, 20)

        but.Location = New Point(358, suma)
        but.Text = "Eliminar"
        but.Name = "but"
        but.Visible = True
        but.Enabled = True
        AddHandler but.Click, AddressOf btn_Click
        Me.Controls.Add(but)
        antcbo = cbo
        antbut = but
        antText = text
        suma = suma + 80
        existe = True
    End Sub
End Class